﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using StudentHookups.WCF;
using System.IO;

namespace StudentHookups.Account.Profile
{
    public partial class EditProfile : System.Web.UI.Page
    {
        WCF.WCFClient client = new WCFClient();

        protected void Page_Load(object sender, EventArgs e)
        {

            for (int i = 18; i <= 99; i++)
            {
                ddlAge.Items.Add(new ListItem(i.ToString()));
            }

            Guid user = (Guid)Membership.GetUser().ProviderUserKey;
            UserComposite loggedinUser = client.GetUser(user);

            if ((loggedinUser == null))
            {
                lblViewProfileWelcome.Text = "To view the rest of this website please fill in all user information";
            }
            else
            {
                if (!IsPostBack)
                {
                    lblViewProfileWelcome.Text = "Your Profile is complete! You can come back to this page at any time to edit your information";
                    tbFirstname.Text = loggedinUser.Firstname;
                    tbLastname.Text = loggedinUser.Lastname;
                    ddlAge.Items.FindByText(loggedinUser.Age.ToString()).Selected = true;
                    ddlChangeGender.Items.FindByText(loggedinUser.Gender).Selected = true;
                    tbInformation.Text = loggedinUser.Information;
                    currImg.ImageUrl = loggedinUser.Image;
                }
            }
        }
        protected void btnSaveProfile_Click(object sender, EventArgs e)
        {

            Guid user = (Guid)Membership.GetUser().ProviderUserKey;
            UserComposite loggedinUser = client.GetUser(user);

            //If its a new user it a new user is creates else it updates the current user
            if ((loggedinUser == null))
            {
                client.AddUser((Guid)Membership.GetUser().ProviderUserKey, Membership.GetUser().UserName, "~/imgs/anonymous.png", tbFirstname.Text,
                tbLastname.Text, ddlChangeGender.SelectedValue, Convert.ToInt16(ddlAge.SelectedValue), null);
            }
            else
            {
                client.UpdateFirstname(loggedinUser.UserID, tbFirstname.Text);
                client.UpdateLastname(loggedinUser.UserID, tbLastname.Text);
                client.UpdateGender(loggedinUser.UserID, ddlChangeGender.SelectedValue);
                client.UpdateAge(loggedinUser.UserID, Convert.ToInt16(ddlAge.SelectedValue));
                lblViewProfileWelcome.Text = "Required Userinformation updated";
               
            }
        }
        protected void saveOptInfo_Click(object sender, EventArgs e)
        {
            Guid user = (Guid)Membership.GetUser().ProviderUserKey;
            UserComposite loggedinUser = client.GetUser(user);

            if ((loggedinUser == null))
            {
                lblViewProfileWelcome.Text = "Fill in required information first";
            }
            else
            {
                client.UpdateInformation(loggedinUser.UserID, tbInformation.Text);
                lblViewProfileWelcome.Text = "Your optional information have now been updated";
                if (imgUpload.HasFile)
                {
                    //splits the file at . making the first part of the string name and second part the file definition
                    string filename = Path.GetFileName(imgUpload.FileName);
                    string[] splitFilename = filename.Split('.');
                    string filepath = "/imgs/" + Membership.GetUser().UserName + "." + splitFilename[1];

                    //Validation to make sure its a image file being uploaded
                    if (splitFilename[1] == "jpeg" || splitFilename[1] == "gif" || splitFilename[1] == "jpg" || splitFilename[1] == "png")
                    {
                        if (File.Exists(filepath))
                        {
                            File.Delete(filepath);
                        }
                        imgUpload.SaveAs(Server.MapPath(filepath));
                        client.UpdateImage(loggedinUser.UserID, filepath);
                        Response.Redirect("~/Account/Profile/EditProfile.aspx");
                        lblViewProfileWelcome.Text = "Succesfully added a new profile image";
                    }

                }

            }

        } 
    }
}